import unittest
import sys,os
sys.path.append(os.path.abspath('..'))
from localEnv import *

class TestLocalEnv(unittest.TestCase):
    """Test Case for localEnv.py"""
    def test_get_name(self):
        "Test get name"
        #print "login name:",get_login_name()
	#print "host name:",get_host_name()
        
    def test_ip_mac(self):
        "Test get ip and mac address"
        #print "eth0 ip:",get_ip("eth0")
        #print "loopback ip:",get_ip("lo")
        self.assertEqual(get_mac("lo"),"no mac")
        #print "eth0 mac:",get_mac("eth0")

    def test_number(self):
        "Test get number function"
        p=get_ps()
        m,n=cpu_mem_usage(p)
        #print "system cpu mem is ",m,n
	name=get_login_name()
        m,n=get_user_cpu_mem_usage(name,p)
        #print "user cpu mem is ",m,n
        m,n=get_net_io("eth0")
        #print "net io eth0",m,n

    def test_get_user_cpu_mem_usage(self):
        "regression test"
        ps = "root 3108 0.0 0.9 7844 1136 ? Ss May12 0:00 sshd: john [priv]\n"
        self.assertEqual(0.9, get_user_cpu_mem_usage('root', ps)[1])
        self.assertEqual(0.0, get_user_cpu_mem_usage('john', ps)[1])  # This..
        # ..used to match at 'john', so tmp[1] was '[priv]', and tmp[2] failed.

    def test_prefix_get_user_cpu_mem_usage(self):
        "regression test with userA prefix of userB"
        ps = """
root2  3106 0.0 0.7 7844 1136 ? Ss May12 0:00 -bash
root3  3107 0.0 0.8 7844 1136 ? Ss May12 0:00 -bash
"""
        self.assertEqual(0.0, get_user_cpu_mem_usage('nonexistant', ps)[1])
        # 1.5 would be the Wrong answer.
        self.assertEqual(0.0, get_user_cpu_mem_usage('root', ps)[1])
        self.assertEqual(0.7, get_user_cpu_mem_usage('root2', ps)[1])
        self.assertEqual(0.8, get_user_cpu_mem_usage('root3', ps)[1])

    def test_get_disk(self):
        "Verify that filesystems allow file creation."
        for line in get_disk().split("\n"):
            if not line.startswith('Filesystem ') and re.search('\s\d', line):
                fs = re.sub('.* ', '', line)  # mount point
                self.assertTrue(os.statvfs(fs).f_bavail > 0)

    def test_usage(self):
        p=get_ps()
        #print "root cpu %f mem %f"%get_user_cpu_mem_usage('root',p)

    def test_cpu(self):
        #print "cpu info",get_cpu()
        #print "up time",get_uptime()
        pass
        
if __name__ == '__main__':
    unittest.main()
